/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package inventory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * rothens.tumblr.com
 * @author Rothens
 */
public class Inventory {
	
	private List<Item> items;
	
	public Inventory(){
		items = new ArrayList<Item>();
	}
	
	public boolean hasItem(String name){
		for(Item i: items){
			
			if(i.name.equalsIgnoreCase(name)){
				
				return true;
			}
		}
		
		return false;
	}
	
	public boolean consume(String name){
		if(!hasItem(name)) return false;
		Iterator<Item> it = items.iterator();
		while(it.hasNext()){
			Item item = it.next();
			if(item.name.equalsIgnoreCase(name)){
				if(!item.consume()){
					it.remove();
				}
				return true;
			}
		}
		return false;		
	}
	
	public boolean addItem(Item item){
		Iterator<Item> it = items.iterator();
		while(it.hasNext()){
			
			Item itm = it.next();
			if(itm.name.equalsIgnoreCase(item.name)){
				int aquire = itm.aquire(item.quantity);
				return aquire == item.quantity;				
			}
		}
		items.add(item);
		return true;
	}
	
	public Item getItem(String name){
		Iterator<Item> it = items.iterator();
		while(it.hasNext()){
			Item item = it.next();
			if(item.name.equalsIgnoreCase(name)){
				return item;
			}
		}
		
		return null;
	}
	
}
